module LogiLang.Parser (
	query,
	assertion,
	rule,
	logifile
	)
	where

import Text.Parsec

import LogiLang.Lexer
import LogiLang.Environment
import LogiLang.Query

singleton = do
	str <- identifier
	return $ Singleton str

combination = do
	children <- parens (many pattern)
	return $ Combination children

variable = do
	reservedOp "?"
	ident <- identifier
	return $ Variable ident

pattern = do
	singleton <|> variable <|> combination

datumcombo = do
	children <- parens (many datum)
	return $ Combination children

datum = do
	singleton <|> datumcombo

simplequery = do
	pat <- pattern
	return $ SimpleQuery pat

connective = 
	choice [do { reserved conn ; return conn } | conn <- ["and", "or", "not"]]

compoundquery = do
	conn <- connective
	subqueries <- parens (many query)
	return $ CompoundQuery conn subqueries

query =
	simplequery <|> compoundquery

assertion = do
	reserved "assert"
	datum >>= return

rule = do
	reserved "rule"
	conclusion <- pattern
	body <- rulebody
	return (conclusion, body)
	where
	rulebody =
		(do { q <- query ; return $ Just q })
		<|>
		(do { reserved "pass" ; return Nothing })

logifile = do
	db <- many assertion
	rules <- many rule
	queries <- many query
	return (db, rules, queries)
	
